package com.vercal.demoverticalviewpager.adapter;

import android.os.Bundle;
import android.os.Parcelable;


import com.vercal.demoverticalviewpager.bean.modelData;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

public class ContentFragmentAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragments = new ArrayList<>();

    private List<modelData> datas=new ArrayList<>();

    private int mChildCount=0;

    public ContentFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<modelData> datas) {
        super(fm);
        this.fragments = fragments;
        this.datas=datas;
    }

    @Override
    public Fragment getItem(int position) {

        Bundle bundle = new Bundle();
        if (datas.size()>0) {
            bundle.putSerializable("modelData", datas.get(position));
            bundle.putInt("position",position);
            fragments.get(position).setArguments(bundle);
        }

        return fragments.get(position);


    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if ( mChildCount > 0) {
            mChildCount --;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}
